package Exercise;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Exercise02 {
    public static void main(String[] args) {
        User user = new User();
        Thread thread = new Thread(user);
        thread.setName("线程1");
        Thread thread1 = new Thread(user);
        thread1.setName("线程2");
        thread.start();
        thread1.start();
    }
}
class User implements Runnable{
    public int money = 10000;
    private boolean flag = true;
    @Override
    public void run() {
        while (flag){
            synchronized (this) {
                if (money <= 0) {
                    System.out.println("金额不足!");
                    flag = false;
                    break;
                }

                money -= 1000;
                System.out.println(Thread.currentThread().getName() + "取走1000元，剩余金额" + (money));
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

}
